package com.mall.product.service.impl;

import com.mall.api.product.dto.ProductDTO;
import com.mall.common.exception.BusinessException;
import com.mall.product.entity.Product;
import com.mall.product.mapper.ProductMapper;
import com.mall.product.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class ProductServiceImpl implements ProductService {

    private final ProductMapper productMapper;

    @Override
    public ProductDTO getProductById(Long id) {
        log.info("获取商品信息: {}", id);
        Product product = productMapper.selectById(id);
        if (product == null) {
            log.error("商品不存在: {}", id);
            throw new BusinessException("商品不存在");
        }
        log.info("商品信息获取成功: {}", product.getName());
        return convertToDTO(product);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createProduct(ProductDTO productDTO) {
        log.info("创建商品: {}", productDTO.getName());
        Product product = new Product();
        BeanUtils.copyProperties(productDTO, product);
        product.setStatus(1);
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());

        productMapper.insert(product);
        log.info("商品创建成功: {}", product.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProduct(ProductDTO productDTO) {
        log.info("更新商品: {}", productDTO.getId());
        Product product = productMapper.selectById(productDTO.getId());
        if (product == null) {
            log.error("商品不存在: {}", productDTO.getId());
            throw new BusinessException("商品不存在");
        }

        BeanUtils.copyProperties(productDTO, product);
        product.setUpdateTime(LocalDateTime.now());

        productMapper.updateById(product);
        log.info("商品更新成功: {}", product.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProduct(Long id) {
        log.info("删除商品: {}", id);
        if (productMapper.deleteById(id) == 0) {
            log.error("商品不存在: {}", id);
            throw new BusinessException("商品不存在");
        }
        log.info("商品删除成功: {}", id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStock(Long id, Integer stock) {
        log.info("更新商品库存: 商品ID: {}, 新库存: {}", id, stock);
        Product product = productMapper.selectById(id);
        if (product == null) {
            log.error("商品不存在: {}", id);
            throw new BusinessException("商品不存在");
        }

        if (stock < 0) {
            log.error("库存不能小于0: 商品ID: {}, 库存: {}", id, stock);
            throw new BusinessException("库存不能小于0");
        }

        product.setStock(stock);
        product.setUpdateTime(LocalDateTime.now());

        productMapper.updateById(product);
        log.info("商品库存更新成功: 商品ID: {}, 新库存: {}", id, stock);
    }

    @Override
    public Integer getStock(Long id) {
        log.info("获取商品库存: {}", id);
        Product product = productMapper.selectById(id);
        if (product == null) {
            log.error("商品不存在: {}", id);
            throw new BusinessException("商品不存在");
        }
        log.info("商品库存获取成功: 商品ID: {}, 库存: {}", id, product.getStock());
        return product.getStock();
    }

    private ProductDTO convertToDTO(Product product) {
        ProductDTO productDTO = new ProductDTO();
        BeanUtils.copyProperties(product, productDTO);
        return productDTO;
    }
} 